/**
 * Created by Administrator on 2017/4/19.
 */
/**
 * Created by Administrator on 2017/2/8.
 */
var type = 2;
$(function () {

    var key = getCookie('key');
    if (!key) {
        window.location.href = WapSiteUrl + '/tmpl/member/login.html';
    }
    function initPage() {
        $.ajax({
            type: 'get',
            url: ApiUrl + "/index.php?act=circle&op=mysend",
            data: {key: key,type:type},
            dataType: 'json',
            success: function (result) {
                if (result.datas.themlist.length) {
                    var discuss = template.render('discuss_tpl', result.datas);
                    $('.list').html(discuss);
                }else{
                    $('.list').html("<p class='noData'>暂无数据</p>");
                }
            }
        });
    }

    //初始化页面
    initPage();
    var index = 1;
    $(window).on('scroll',function() {
        var scrollTop = parseInt($(this).scrollTop());
        var scrollHeight = $(document).height();
        var windowHeight = $(this).height();
        if (scrollTop + windowHeight >= scrollHeight - 1) {
            index++;
            $.ajax({
                type: 'get',
                url: ApiUrl + "/index.php?act=circle&op=mysend&page="+index,
                data: {key: key,type:type},
                dataType: 'json',
                success: function (result) {
                    console.log(result);
                    var discuss = template.render('discuss_tpl',result.datas);
                    $('.list').html($('.list').html()+discuss);
                }
            });
        }
    })

    var data = 1;
    //初始化页面
    initPage();
    var index = 1;
    $(window).on('scroll',function() {
        var scrollTop = parseInt($(this).scrollTop());
        var scrollHeight = $(document).height();
        var windowHeight = $(this).height();
        if (scrollTop + windowHeight >= scrollHeight - 1) {
            index++;
            if(data==1){
                $.ajax({
                    type: 'get',
                    url: ApiUrl + "/index.php?act=circle&op=mysend&page="+index,
                    data: {key: key,type:type},
                    dataType: 'json',
                    success: function (result) {
                        console.log(result);
                        var discuss = template.render('discuss_tpl',result.datas);
                        $('.list').html($('.list').html()+discuss);
                    }
                });
            }else{
                $.ajax({
                    type: 'get',
                    url: ApiUrl + "/index.php?act=circle&op=myreplay&page="+index,
                    data: {key: key,c_id:type},
                    dataType: 'json',
                    success: function (result) {
                        var discuss = template.render('reply_tpl',result.datas);
                        $('.list').html($('.list').html()+discuss);
                    }
                });
            }

        }
    })

    // 获取回复信息
    function getReplay(){
        $.ajax({
            type: 'get',
            url: ApiUrl + "/index.php?act=circle&op=myreplay",
            data: {key: key,c_id:type},
            dataType: 'json',
            success: function (result) {
                if (result.datas.themlist.length) {
                    var discuss = template.render('reply_tpl', result.datas);
                    $('.list').html(discuss);
                }else{
                    $('.list').html("<p class='noData'>暂无数据</p>");
                }
            }
        });
    }

    $("#filtrate_ul li").on('click',function (){
        var id = $(this).attr('data-id');
        data = id;
        $(this).addClass('selected').siblings().removeClass('selected');
        if(id==1){
            initPage();
        }else{
            getReplay();
        }
    })
});
//删除
function delById(_this,id){
    $.sDialog({
        skin:"red",
        content:"确定删除",
        okBtn:true,
        cancelBtn:true,
        "okBtnText": "确定", //确定按钮的文字
        "cancelBtnText": "取消", //取消按钮的文字
        "okFn": function() {
            $.ajax({
                type:'get',
                url:ApiUrl+"/index.php?act=circle&op=del_theme",
                data:{id:id},
                dataType:"json",
                success:function(res){
                    if(res.datas.status==1){
                        creatLayout('删除成功');
                        var parent = _this.parentNode.parentNode.parentNode;
                        document.getElementsByClassName('list')[0].removeChild(parent);
                    }
                }
            })
        }, //点击确定按钮执行的函数
    });
}